//
//  ColumnController.m
//  dfdsf
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "ColumnController.h"
#import "ModelForColumn.h"
#import "CellOfColumn.h"
#import <DKNightVersion.h>
#import "CellForColumCollection.h"
#import "Column2Controller.h"
#import "UIColor+Wonderful.h"
#import <DKNightVersion.h>
#import "UIColor+Wonderful.h"

#define WEIGHTT [UIScreen mainScreen].bounds.size.width / 375
#define HEIGHTT [UIScreen mainScreen].bounds.size.height / 667


@interface ColumnController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *viewForTitle;


@end

@implementation ColumnController
- (void)dealloc {
 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"next" object:nil];
    [_array release];
    [_viewForTitle release];
    [_tableView release];
    [super dealloc];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handelData];
    [self createTable];
    [self createTitleView];
    
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"next" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
        
       
          Column2Controller *column2 = [[Column2Controller alloc] init];
        NSString *str = @"http://platform.sina.com.cn/sports_client/z_feed_list?app_key=2586208540&_version__=3.2.1.01&__os__=android&id=";
        
        NSString *str2 = @"&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221456998464053%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22ZTE+G717C%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.2.2%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%94%B5%E4%BF%A1%22%2C%22device_id%22%3A%22A0000038A97F12%22%2C%22make%22%3A%22ZTE+G717C%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30201001%2C%22ip%22%3A%22fe80%3A%3A4ecb%3Af5ff%3Afe3e%3Ae562%25wlan0%22%7D%7D&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
        
        NSString *modelID = [note.userInfo objectForKey:@"ID"];
        NSString *url = [str stringByAppendingFormat:@"%@%@", modelID, str2];
        column2.url = url;
        
        NSString *read = [note.userInfo objectForKey:@"read"];
        column2.read = [NSString stringWithFormat:@"%@", read];
        
        [self.navigationController pushViewController:column2 animated:NO];
        [column2 release];
        
    }];

    
    
}


/** 头部的标题 -- 专栏*/
- (void)createTitleView {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor deepSkyBlue], [UIColor lightGrayColor]);

    
 self.navigationItem.title = @"专栏";
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addLikeAction:)] autorelease];
    
    

}


- (void)addLikeAction:(UIBarButtonItem *) item {

 


}


- (void)handelData {

 NSString *str = @"http://platform.sina.com.cn/sports_client/z_list";
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
    request.HTTPMethod = @"POST";
    request.HTTPBody = [@"app_key=2586208540&_version__=3.2.1.01&__os__=android" dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        dispatch_async(dispatch_get_main_queue(), ^{

            id result = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error:nil];
            //NSLog(@"%@", result);
            
            self.array = [NSMutableArray array];
            
            NSDictionary *dic = [result objectForKey:@"result"];
            
            NSDictionary *dict = [dic objectForKey:@"data"];
            
            
            NSArray *array = [dict objectForKey:@"list"];

            
            for (NSDictionary *dicd in array) {
                
                ModelForColumn *model = [[ModelForColumn alloc] init];
                
                [model setValuesForKeysWithDictionary:dicd];
                
                
                [self.array addObject:model];
                
                [model release];
            }
            [self.array removeObjectAtIndex:0];
            [self.tableView reloadData];
       });
        
        
    }];
    
    [task resume];

}


- (void) createTable {


    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
     
    [self.tableView registerClass:[CellForColumCollection class] forCellReuseIdentifier:@"poolForCollection"];
    [self.tableView registerClass:[CellOfColumn class] forCellReuseIdentifier:@"poolForColumn"];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 2;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (section == 0) {
        
        return @"热门推荐";
    } else {
    
    return @"全部专栏";
    
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    if (section == 0) {
     
        return 1;
        
    } else {
     
    return self.array.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


    if (indexPath.section == 0) {
        
       CellForColumCollection *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForCollection"];
        
        return cell;
        
    } else {
    
   CellOfColumn *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForColumn"];


    ModelForColumn *model = [self.array objectAtIndex:indexPath.row];

    [cell passModelForColumn:model];


    return cell;
    
}

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
     
        
        return self.view.frame.size.height / 6;
    }
    else {
    return self.view.frame.size.height / 8;
    }

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    Column2Controller *column2 = [[Column2Controller alloc] init];
    
    NSString *str = @"http://platform.sina.com.cn/sports_client/z_feed_list?app_key=2586208540&_version__=3.2.1.01&__os__=android&id=";
    
    NSString *str2 = @"&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%221456998464053%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22ZTE+G717C%22%2C%22device_type%22%3A%224%22%2C%22osv%22%3A%224.2.2%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%94%B5%E4%BF%A1%22%2C%22device_id%22%3A%22A0000038A97F12%22%2C%22make%22%3A%22ZTE+G717C%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22version%22%3A30201001%2C%22ip%22%3A%22fe80%3A%3A4ecb%3Af5ff%3Afe3e%3Ae562%25wlan0%22%7D%7D&client_deviceid=7005145ee5bbe7f1f97c5e227f4ab771";
    
    ModelForColumn *model = [self.array objectAtIndex:indexPath.row];
    
    
    NSString *url = [str stringByAppendingFormat:@"%@%@", model._id, str2];
    
    
    column2.url = url;
    column2.read = [NSString stringWithFormat:@"%ld", model.fans_num];
    
    [self.navigationController pushViewController:column2 animated:NO];
     
    [column2 release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
